package yf.mall.component.helper;

import com.google.common.collect.Lists;

import com.yf.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import yf.mall.entity.KolProductStatEntity;
import yf.mall.enums.app.AppDateTypeEnum;
import yf.mall.utils.app.AppUtil;
import yf.mall.dysdk.model.product.DailyStatistics;
import yf.mall.dysdk.model.product.DyProduct;
import yf.mall.dysdk.model.product.Score;
import yf.mall.dysdk.model.product.ShopTotalScore;
import yf.mall.entity.KolProductEntity;
import yf.mall.entity.KolShopEntity;
import yf.mall.vo.app.product.DailyStatisticsVo;
import yf.mall.vo.app.product.KolProductVo;
import yf.mall.vo.app.product.KolShopVo;
import yf.mall.vo.app.product.TotalStatisticsVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yxmds
 */
public final class ProductHelper {


    public static KolProductVo convertProductEnityToVo(KolProductEntity kolProduct) {
        KolProductVo kolProductVo = new KolProductVo();
        kolProductVo.setAddTime(kolProduct.getAddTime().toString());
        kolProductVo.setCosFee(AppUtil.getNumberStr(kolProduct.getCosFee()));
        kolProductVo.setCosRatio(kolProduct.getCosRatio().toString());
        kolProductVo.setCover(kolProduct.getCover());
        kolProductVo.setKolCosFee(AppUtil.getNumberStr(kolProduct.getKolCosFee()));
        kolProductVo.setKolCosRatio(kolProduct.getKolCosRatio().toString());
        kolProductVo.setPid(kolProduct.getId().toString());
        kolProductVo.setPrice(AppUtil.getNumberStr(kolProduct.getPrice()));
        kolProductVo.setProductId(kolProduct.getId().toString());
        kolProductVo.setSales(AppUtil.getNumberStrToWan(BigDecimal.valueOf(kolProduct.getSales()), 0));
        kolProductVo.setTitle(kolProduct.getTitle());
        kolProductVo.setUrl(kolProduct.getUrl());
        kolProductVo.setFirstCid(kolProduct.getFirstCid());
        kolProductVo.setSecondCid(kolProduct.getSecondCid());
        kolProductVo.setThirdCid(kolProduct.getThirdCid());
        kolProductVo.setDay30Sales(AppUtil.getNumberStrToWan(BigDecimal.valueOf(kolProduct.getOrderNum()), 0));
        if (StringUtils.isNotBlank(kolProduct.getCoverImgMaterialUrl())) {
            kolProductVo.setImages(Stream.of(kolProduct.getCoverImgMaterialUrl().split(",")).collect(Collectors.toList()));
        }
        if (StringUtils.isNotBlank(kolProduct.getDetailImgMaterialUrl())) {
            kolProductVo.setDetailImage(Stream.of(kolProduct.getDetailImgMaterialUrl().split(",")).collect(Collectors.toList()));
        }
        kolProductVo.setPromotion(kolProduct.getPromotion());
        kolProductVo.setVideoUrl(kolProduct.getVideoUrl());
        kolProductVo.setKol(kolProduct.getKolCosRatio() != null && kolProduct.getKolCosRatio() > 30);
        return kolProductVo;
    }

    public static KolShopVo convertShopEntityToVo(KolShopEntity kolShop) {
        KolShopVo kolShopVo = new KolShopVo();
        kolShopVo.setLogo(kolShop.getLogo());
        kolShopVo.setName(kolShop.getName());
        kolShopVo.setLogisticsRating(AppUtil.convertScoreLv(kolShop.getLogisticsRating()));
        kolShopVo.setLogisticsScore(String.valueOf(kolShop.getLogisticsScore().intValue()));
        kolShopVo.setProductRating(AppUtil.convertScoreLv(kolShop.getProductRating()));
        kolShopVo.setProductScore(String.valueOf(kolShop.getProductScore().intValue()));
        kolShopVo.setScore(kolShop.getScore().intValue());
        kolShopVo.setScoreRating(AppUtil.convertScoreLv(kolShop.getScoreRating()));
        kolShopVo.setShopRating(AppUtil.convertScoreLv(kolShop.getShopRating()));
        kolShopVo.setShopScore(String.valueOf(kolShop.getShopScore().intValue()));
        kolShopVo.setServiceRating(AppUtil.convertScoreLv(kolShop.getServiceRating()));
        kolShopVo.setServiceScore(String.valueOf(kolShop.getServiceScore().intValue()));
        return kolShopVo;
    }

    public static List<KolProductVo> productEntityListToVo(List<KolProductEntity> kolProductEntities) {
        return kolProductEntities.stream().map(ProductHelper::convertProductEnityToVo).collect(Collectors.toList());
    }

    public static List<KolProductEntity> dyProductListConvertToEntity(List<DyProduct> products) {
        return products.stream().map(ProductHelper::convertProductDyToDb).collect(Collectors.toList());
    }

    public static TotalStatisticsVo convertStatistics(DyProduct dyProduct, Date begin, Date end) {
        DailyStatistics[] dailyStatistics = dyProduct.getDailyStatistics();
        TotalStatisticsVo totalStatisticsVo = new TotalStatisticsVo();
        List<DailyStatisticsVo> datas = new ArrayList<>();
        Integer totalKolNum = 0;
        Integer totalKolViews = 0;
        Integer totalSales = 0;
        for (DailyStatistics statistics : dailyStatistics) {
            Date day = statistics.getDate();
            if (day.compareTo(begin) < 0 || day.compareTo(end) > 0) {
                continue;
            }
            DailyStatisticsVo dailyStatisticsVo = new DailyStatisticsVo();
            dailyStatisticsVo.setAddTime(DateUtil.dateFormat("yyyy-MM-dd", day));
            dailyStatisticsVo.setKolNum(statistics.getKolNum());
            dailyStatisticsVo.setSales(statistics.getOrderNum());
            dailyStatisticsVo.setViews(statistics.getViewNum());
            // 转化率= 订单数/观看数
            dailyStatisticsVo.setCvr(AppUtil.calRatio(statistics.getOrderNum(), statistics.getViewNum()));
            datas.add(dailyStatisticsVo);
            totalKolNum += statistics.getKolNum();
            totalKolViews += statistics.getViewNum();
            totalSales += statistics.getOrderNum();
        }
        totalStatisticsVo.setData(datas);
        totalStatisticsVo.setTotalKolNum(totalKolNum);
        totalStatisticsVo.setTotalKolViews(totalKolViews);
        totalStatisticsVo.setTotalSales(totalSales);
        totalStatisticsVo.setTotalCvr(AppUtil.calRatio(totalSales, totalKolViews));
        return totalStatisticsVo;
    }


    public static KolProductEntity convertProductDyToDb(DyProduct dyProduct) {
        KolProductEntity productEntity = new KolProductEntity();
        productEntity.setId(dyProduct.getProductId());
        productEntity.setInStock(dyProduct.getInStock()?1:0);
        productEntity.setTitle(dyProduct.getTitle());
        productEntity.setAddTime(new Date());
        productEntity.setFirstCid(dyProduct.getFirstCid().longValue());
        productEntity.setSecondCid(dyProduct.getSecondCid().longValue());
        if (dyProduct.getThirdCid() != null) {
            productEntity.setThirdCid(dyProduct.getThirdCid().longValue());
        } else {
            productEntity.setThirdCid(0L);
        }
        productEntity.setSales(dyProduct.getSales().longValue());
        productEntity.setCover(dyProduct.getCover());
        productEntity.setCoverImgMaterialUrl(Stream.of(dyProduct.getImgs()).limit(5).collect(Collectors.joining(",")));
        productEntity.setUrl(dyProduct.getDetailUrl());
        productEntity.setShopId(dyProduct.getShopId().longValue());
        productEntity.setPrice(BigDecimal.valueOf(dyProduct.getPrice()));
        productEntity.setCosRatio(dyProduct.getCosRatio());
        productEntity.setCosFee(BigDecimal.valueOf(dyProduct.getCosFee()));
        productEntity.setKolCosRatio(dyProduct.getKolCosRatio());
        productEntity.setKolCosFee(BigDecimal.valueOf(dyProduct.getKolCosFee()));
        productEntity.setCommentScore(BigDecimal.valueOf(dyProduct.getCommentScore()));
        productEntity.setCommentNum(Long.valueOf(dyProduct.getCommentNum()));
        productEntity.setOrderNum(Long.valueOf(dyProduct.getOrderNum()));
        productEntity.setViewNum(Long.valueOf(dyProduct.getViewNum()));
        productEntity.setKolNum(Long.valueOf(dyProduct.getKolNum()));
//        productEntity.setMarketType(0);
        productEntity.setCategoryName(dyProduct.getCategoryName());
        productEntity.setLogisticsInfo(dyProduct.getLogisticsInfo());
        productEntity.setAssured(dyProduct.getIsAssured() ? 1 : 0);
        productEntity.setSxt(dyProduct.getHasSxt() ? 1 : 0);
        productEntity.setSharable(0);
        productEntity.setRecommend(0);
        String detailImages = dyProduct.getDetailBrief().getComponentInfo().stream().map(componentInfo -> componentInfo.getImage().getPicUrl()).collect(Collectors.joining(","));
        productEntity.setDetailImgMaterialUrl(detailImages);

        DailyStatistics[] dailyStatistics = dyProduct.getDailyStatistics();
        Date saleOf7Begin = AppUtil.getBeginByType(AppDateTypeEnum.DAY_7.getType());
        Date saleOfTodayBegin = AppUtil.getBeginByType(AppDateTypeEnum.TODAY.getType());
        long saleOf7 = 0L;
        long saleOfToday = 0L;
        for (DailyStatistics dayData : dailyStatistics) {
            if (dayData.getDate().compareTo(saleOf7Begin) >= 0) {
                saleOf7 += dayData.getOrderNum();
            }
            if (dayData.getDate().compareTo(saleOfTodayBegin) == 0) {
                saleOfToday += dayData.getOrderNum();
            }
        }
        productEntity.setSaleOf7(saleOf7);
        productEntity.setSaleOfToday(saleOfToday);
        return productEntity;
    }

    public static KolShopEntity convertShopDyToDb(DyProduct dyProduct) {
        KolShopEntity shopEntity = new KolShopEntity();
        shopEntity.setId(Long.valueOf(dyProduct.getShopId()));
        shopEntity.setName(dyProduct.getShopName());
        shopEntity.setLogo(""); // 设置默认logo
        ShopTotalScore shopTotalScore = dyProduct.getShopTotalScore();
        Score logisticsScore = shopTotalScore.getLogisticsScore();
        shopEntity.setLogisticsRating(String.valueOf(logisticsScore.getLevel()));
        shopEntity.setLogisticsScore(new BigDecimal(logisticsScore.getScore()));
        Score productScore = shopTotalScore.getProductScore();
        shopEntity.setProductRating(String.valueOf(productScore.getLevel()));
        shopEntity.setProductScore(new BigDecimal(productScore.getScore()));
        Score serviceScore = shopTotalScore.getServiceScore();
        shopEntity.setServiceRating(String.valueOf(serviceScore.getLevel()));
        shopEntity.setServiceScore(new BigDecimal(serviceScore.getScore()));
        Score shopScore = shopTotalScore.getShopScore();
        shopEntity.setShopRating(String.valueOf(logisticsScore.getLevel()));
        shopEntity.setShopScore(new BigDecimal(shopScore.getScore()));
        shopEntity.setScore(new BigDecimal(shopScore.getScore()));
        shopEntity.setScoreRating(String.valueOf(shopScore.getLevel()));
        shopEntity.setRemark("");
        return shopEntity;
    }

    public static TotalStatisticsVo convert24Stat(KolProductStatEntity kolProductStatEntity, Integer beforeHour) {
        TotalStatisticsVo totalStatisticsVo = new TotalStatisticsVo();
        List<DailyStatisticsVo> list = convertDb24StatToList(kolProductStatEntity, beforeHour);
        totalStatisticsVo.setData(list);
        totalStatisticsVo.setTotalSales(0);
        return totalStatisticsVo;
    }

    private static List<DailyStatisticsVo> convertDb24StatToList(KolProductStatEntity stat, Integer beforeHour) {
        List<DailyStatisticsVo> list = new ArrayList<>();
        if (beforeHour == 2) {
            list.add(new DailyStatisticsVo(stat.getSales3Hour(), AppUtil.getBeforeTimeStr(3)));
            list.add(new DailyStatisticsVo(stat.getSales2Hour(), AppUtil.getBeforeTimeStr(2)));
            list.add(new DailyStatisticsVo(stat.getSales1Hour(), AppUtil.getBeforeTimeStr(1)));
        } else {
            list.add(new DailyStatisticsVo(stat.getSales23Hour(), AppUtil.getBeforeTimeStr(23)));
            list.add(new DailyStatisticsVo(stat.getSales21Hour(), AppUtil.getBeforeTimeStr(21)));
            list.add(new DailyStatisticsVo(stat.getSales19Hour(), AppUtil.getBeforeTimeStr(19)));
            list.add(new DailyStatisticsVo(stat.getSales17Hour(), AppUtil.getBeforeTimeStr(17)));
            list.add(new DailyStatisticsVo(stat.getSales15Hour(), AppUtil.getBeforeTimeStr(15)));
            list.add(new DailyStatisticsVo(stat.getSales13Hour(), AppUtil.getBeforeTimeStr(13)));
            list.add(new DailyStatisticsVo(stat.getSales11Hour(), AppUtil.getBeforeTimeStr(11)));
            list.add(new DailyStatisticsVo(stat.getSales9Hour(), AppUtil.getBeforeTimeStr(9)));
            list.add(new DailyStatisticsVo(stat.getSales7Hour(), AppUtil.getBeforeTimeStr(7)));
            list.add(new DailyStatisticsVo(stat.getSales5Hour(), AppUtil.getBeforeTimeStr(5)));
            list.add(new DailyStatisticsVo(stat.getSales3Hour(), AppUtil.getBeforeTimeStr(3)));
            list.add(new DailyStatisticsVo(stat.getSales1Hour(), AppUtil.getBeforeTimeStr(1)));
        }
        return list;
    }
}
